<?php


namespace App\Service;


use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\FlyGourmetReservationConf;
use Carbon\Carbon;

/**
 * 商户美食点座配置
 * Class FlyGourmetReservationConfService
 * @package App\Service
 */
class FlyGourmetReservationConfService extends BaseService
{

    /**
     * 保存配置
     * saveing
     * @param int $shopId
     * @param array $args
     * @return \Hyperf\Utils\HigherOrderTapProxy|int|mixed|void
     * date:2022/7/19
     * time:15:43
     * auth：xyc
     */
    public function saveing(int $shopId, array $args): int
    {
        $flyGourmetReservationConf = FlyGourmetReservationConf::query()->where('shop_id', $shopId)->first();

        //初始化
        if ($flyGourmetReservationConf == null) {
            $flyGourmetReservationConf = FlyGourmetReservationConf::make();
            $flyGourmetReservationConf->shop_id = $shopId;
        }
        $flyGourmetReservationConf->fill($args);

        //如果没有包间
        if (!$flyGourmetReservationConf->has_rooms) {
            $flyGourmetReservationConf->min_number = 0;
        }


        //如果定金类型不是免费
        if ($flyGourmetReservationConf->deposit_type == FlyGourmetReservationConf::DEPOSIT_TYPE_FREE) {
            $flyGourmetReservationConf->down_payment_yuan = 0;
        }

        //如果随时可退得话 不需要时间了
        if ($flyGourmetReservationConf->refund_type == FlyGourmetReservationConf::REFUND_TYPE_RETUTNABLE) {
            $flyGourmetReservationConf->refundable_time_minute = 0;
        }

        if (!$flyGourmetReservationConf->save()) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, "保存配置失败!");
        }
        return (int)$flyGourmetReservationConf->id;
    }


    /**
     * 获取商户订座价格
     * getDownPaymentPrice
     * @param FlyGourmetReservationConf|null $flyGourmetReservationConf
     * @param int $peole
     * @return float
     * date:2022/7/19
     * time:15:54
     * auth：xyc
     */
    public function getDownPaymentPrice(?FlyGourmetReservationConf $flyGourmetReservationConf, int $peole = 0): float
    {
        if ($flyGourmetReservationConf != null) {
            switch ($flyGourmetReservationConf->deposit_type) {
                case FlyGourmetReservationConf::DEPOSIT_TYPE_FREE:
                    return 0;
                case FlyGourmetReservationConf::DEPOSIT_TYPE_PEOPLE:
                    return floatval(bcmul(strval($flyGourmetReservationConf->down_payment_yuan), strval($peole), 2));
                case FlyGourmetReservationConf::DEPOSIT_TYPE_ORDER:
                    return floatval($flyGourmetReservationConf->down_payment_yuan);

            }
            throw new BusinessException(ErrorCode::SERVER_ERROR, "获取商户配置价格错误!");
        }
        throw new BusinessException(ErrorCode::SERVER_ERROR, "未获取该商户的配置!");

    }


    /**
     * 是否可以自动退款
     * isRefund
     * @param FlyGourmetReservationConf|null $flyGourmetReservationConf
     * @param Carbon $scheduledTime 预定时间
     * @param Carbon|null $currTime 默认当前时间
     * @return bool
     * date:2022/7/19
     * time:16:21
     * auth：xyc
     */
    public function isRefund(?FlyGourmetReservationConf $flyGourmetReservationConf, Carbon $scheduledTime, Carbon $currTime = null): bool
    {
        //规定时间内多少分钟
        if ($flyGourmetReservationConf->refund_type == FlyGourmetReservationConf::REFUND_TYPE_SCHEDULT_TIME) {
            $currTime = $currTime ?: Carbon::now();
            $diffInMinutes = $scheduledTime->diffInMinutes($currTime, false); //预定时间-当前时间的分钟
            return $diffInMinutes >= intval($flyGourmetReservationConf->refundable_time_minute);

        }
        return true;
    }
}